import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';
import 'package:password_input/password_input.dart';

class DialogUtil {
  static Future<void> dismiss({
    SmartStatus status = SmartStatus.smart,
  }) =>
      SmartDialog.dismiss(status: status);

  static Future showCustomDialog(
    Widget dialog, {
    Alignment? alignment,
    VoidCallback? onDismiss,
    bool clickDismiss = false,
    String? tag,
    BuildContext? bindWidget,
    SmartDialogController? controller,
    bool? backDismiss,
  }) async {
    await SmartDialog.show(
      builder: (ctx) => dialog,
      alignment: alignment ?? const Alignment(0.0, 1),
      onDismiss: onDismiss,
      clickMaskDismiss: clickDismiss,
      tag: tag,
      bindWidget: bindWidget,
      controller: controller,
      backDismiss: backDismiss,
    );
  }

  static void showLoading({String? msg, Alignment? alignment, Widget? widget}) {
    SmartDialog.showLoading(msg: msg ?? "加载中...");
  }

  static Future<void> showPwdDialog(ValueChanged<String> callback,
      {double? amount}) async {
    final focusNode = FocusNode();
    focusNode.requestFocus();
    await DialogUtil.showCustomDialog(
        Container(
          margin: MarginPaddingUtil.symmetric(horizontal: 65),
          padding: MarginPaddingUtil.symmetric(horizontal: 36, vertical: 70),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.sp),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  TextCommon(
                    "请输入支付密码",
                    size: 32.sp,
                    color: Color(0xFF414141),
                    fontWeight: FontWeight.bold,
                  ),
                  Spacer(),
                  ImageCommon(
                    "/icon/icon_close",
                    size: 26.width,
                  ).addClickEvent(() => DialogUtil.dismiss())
                ],
              ),
              if (amount != null)
                Column(
                  children: [
                    31.heightBox,
                    TextCommon(
                      "$amount",
                      size: 50.sp,
                      lineHeight: 72.sp,
                      color: Color(0xFF414141),
                      fontWeight: FontWeight.bold,
                    ),
                    TextCommon(
                      "酝积分",
                      size: 24.sp,
                      lineHeight: 35.sp,
                      color: Color(0xFF414141),
                    ),
                  ],
                ),
              40.heightBox,
              SizedBox(
                height: 74.width,
                child: PasswordInputTextField(
                  passwordLength: 6,
                  autoFocus: true,
                  keyboardType: TextInputType.number,
                  focusNode: focusNode,
                  inputFormatter: [
                    FilteringTextInputFormatter.allow(
                        RegExp(r'[a-zA-Z0-9_@.]' r''))
                  ],
                  decoration: BoxLooseDecoration(
                      gapSpace: 12.width,
                      strokeColor: Colors.transparent,
                      solidColor: Color(0xFFF6F6F6),
                      radius: Radius.circular(10.radius),
                      obscureStyle: ObscureStyle(isTextObscure: true),
                      textStyle: TextStyle(
                        fontSize: 60.sp,
                        color: Colors.black,
                        height: 2,
                      )),
                  onChanged: (password) async {
                    if (password.length == 6) {
                      FocusScope.of(Get.context!).unfocus();
                      await DialogUtil.dismiss();
                      callback.call(password);
                    }
                  },
                ),
              )
            ],
          ),
        ),
        alignment: Alignment(0, -0.5));
  }
}
